var teaInfo = []; //茶叶的信息(id,name,price...)
var wrapInfo = []; //包装基本信息
var expressInfo = []; //快递信息

$(function() {
	initTeaInfo();
	initWrapInfo();
	initExpressInfo();
	recipientAddress();
})

/**
 * 初始化茶叶信息
 */
function initTeaInfo() {
	Util.ajax("/base/tea", null, function(res) {
		teaInfo = res;
		var html = creareTeaSelectHtml(res);
		$(".item-box select:eq(0)").html(html);
	})
}
/**
 * 初始化包装信息
 */
function initWrapInfo(){
	Util.ajax("/base/wrap", null, function(res) {
		wrapInfo = res;
		var html = creareWrapSelectHtml(res);
		$(".item-box select:eq(1)").html(html); 
	})
}

/**
 * 初始化快递信息
 */
function initExpressInfo(){
	
}
/**
 * 动态生成茶叶信息下来列表
 * @param {Object} array
 */
function creareTeaSelectHtml(array) {
	var html = '<option value="0">茶叶分类</option>';
	for(var i = 0; i < array.length; i++) {
		var tea = array[i];
		html += '<option value="' + tea.id + '">' + tea.name + '</option>';
	}
	return html;
}



/**
 * 动态生成包装信息下来列表
 * @param {Object} array
 */
function creareWrapSelectHtml(array) {
	var html = '<option value="0">包装分类</option>';
	for(var i = 0; i < array.length; i++) {
		var wrap = array[i];
		html += '<option value="' + wrap.id + '">' + wrap.name + '</option>';
	}
	return html;
}
/**
 * 初始化收货人地址选择框
 */
function recipientAddress() {
	initRecipientProvice()
	initRecipientCity();

}

function initRecipientProvice() {
	var province = ChinaCity.getProvince();
	var provinceHtml = [];
	for(var i = 0; i < province.length; i++) {
		var html = '<option value=' + province[i] + '>' + province[i] + '</option>';
		if(province[i] == "四川") {
			html = '<option value=' + province[i] + ' selected="true">' + province[i] + '</option>';
		}
		provinceHtml.push(html);
	}
	$("#recipientProvice").html(provinceHtml.join(''));
	changeExpressMoney();
}

function initRecipientCity() {
	var province = $("#recipientProvice").val();
	var city = ChinaCity.getCity(province);
	var cityHtml = [];
	for(var i = 0; i < city.length; i++) {
		var html = '<option value=' + city[i] + '>' + city[i] + '</option>';
		cityHtml.push(html);
	}
	$("#recipientCity").html(cityHtml.join(''));
	initRecipientArea();
}

function initRecipientArea() {
	var province = $("#recipientProvice").val();
	var city = $("#recipientCity").val();
	var areas = ChinaCity.getArea(province, city);
	var areaHtml = [];
	for(var i = 0; i < areas.length; i++) {
		var html = '<option value=' + areas[i] + '>' + areas[i] + '</option>';
		areaHtml.push(html);
	}
	$("#recipientArea").html(areaHtml.join(''));
}

/**
 * 创建订单
 */
function createOrder() {
	var goods = $(".goods-box-each");
	var goodArray = [];
	for(var i = 0; i < goods.length; i++) {
		var teaBox = $(goods[i]).find(".item-box")[0];
		var teaId = $(teaBox).find("select").val();
		var teaWeight = $(teaBox).find("input:eq(0)").val();
		var teaMoney = $(teaBox).find("input:eq(1)").val();
		var wrapBox = $(goods[i]).find(".item-box")[1];
		var wrapId = $(wrapBox).find("select").val();
		var wrapAmount = $(wrapBox).find("input:eq(0)").val();
		var wrapMoney = $(wrapBox).find("input:eq(1)").val();
		if(Util.isEmpty(teaId) || Util.isEmpty(teaWeight) || Util.isEmpty(teaMoney)) {
			alert("茶叶信息没有填写完整")
			return;
		}
		//包装可以不选
		//		if(Util.isEmpty(wrapType) || Util.isEmpty(wrapAmoun) || Util.isEmpty(wrapMoney)) {
		//			alert("茶叶信息没有填写完整")
		//			return;
		//		}
		//
		var good = {
			"teaId": teaId,
			"teaWeight": teaWeight,
			"teaMoney": teaMoney,
			"wrapId": wrapId,
			"wrapAmount": wrapAmount,
			"wrapMoney": wrapMoney
		};
		goodArray.push(good);
	}
	var agentName = $("#agentName").val();
	var agentPhone = $("#agentPhone").val();
	agentName = Util.isEmpty(agentName) ? "" : agentName;
	agentPhone = Util.isEmpty(agentPhone) ? "" : agentPhone;
	
	var expressType = $("#expressType").val();
	var expressMoney = $("#expressMoney").val();
	
	var recipientName = $("#recipientName").val();
	var recipientPhone = $("#recipientPhone").val();
	var recipientProvice = $("#recipientProvice").val();
	var recipientCity = $("#recipientCity").val();
	var recipientArea = $("#recipientArea").val();
	var recipientAddres = $("#recipientAddress").val();
	var recipientRemak = $("#recipientRemak").val();
	if(Util.isEmpty(recipientName) || Util.isEmpty(recipientPhone) || Util.isEmpty(recipientProvice) ||
		Util.isEmpty(recipientCity) || Util.isEmpty(recipientArea) || Util.isEmpty(recipientAddres)) {
		alert("收货人信息不完整");
		return;
	}
//	var postName = $("#postName").val();
//	var postPhone = $("#postPhone").val();
	// 提交请求
	var param = {"goodArray":JSON.stringify(goodArray),"agentName":agentName,"agentPhone":agentPhone,"expressType":expressType,
		"expressMoney":expressMoney,"recipientName":recipientName,"recipientPhone":recipientPhone,
		"recipientProvice":recipientProvice,"recipientCity":recipientCity,"recipientArea":recipientArea,
		"recipientAddres":recipientAddres,"recipientRemak":recipientRemak};
	Util.ajax("order/create",param,function(res){
		var errorCode = res.errorCode;
		if(errorCode == 0){
			alert("订单创建成功");
		}else{
			alert("订单创建失败")
		}
	
	})

}


/**
 * 动态添加订单内容
 */
function addGoodItem() {
	var html = $(".goods-box-each").parent().children(":last").clone(false);
	$(html).find("input").val(0);
	$(".goods-box").append(html);
	$(".goods-box").css("height", 95 + $(".goods-box").height());
}

/**
 * 监听茶叶重量变化
 * @param {Object} e
 */
function listenerTeaWeightChange(e){
	var weight = $(e).parent().parent().find("input:eq(0)").val();
	var teaType = $(e).parent().parent().find("select").val();
	var money = evalTeaMoney(teaType,weight);
	var div = $(e).parent().parent().find("input:eq(1)").val(money);
	changeExpressMoney();
}

/**
 * 计算茶叶价格
 * @param {Object} teatType
 * @param {Object} weight
 */
function evalTeaMoney(teatType,weight){
	if(teatType == 0 || weight == 0){
		return 0;
	}
	var price = 0;
	for(var i = 0; i < teaInfo.length; i++){
		if(teaInfo[i].id == teatType){
			price = teaInfo[i].sellPrice;
			break;
		}
	}
	return price * weight;
}

/**
 * 监听包装数量变化
 * @param {Object} e
 */
function listenerWrapWeightChange(e){
	var amount = $(e).parent().parent().find("input:eq(0)").val();
	var wrapType = $(e).parent().parent().find("select").val();
	var money = evalWrapMoney(wrapType,amount);
	var div = $(e).parent().parent().find("input:eq(1)").val(money);
	changeExpressMoney();
	
}

/**
 * 计算包装价格
 * @param {Object} wrapType
 * @param {Object} amount
 */
function evalWrapMoney(wrapType,amount){
	if(wrapType == 0 || amount == 0){
		return 0;
	}
	var price = 0;
	for(var i = 0; i < wrapInfo.length; i++){
		if(wrapInfo[i].id == wrapType){
			price = wrapInfo[i].sellPrice;
			break;
		}
	}
	return price * amount;
}

/**
 * 改变快递费变化
 */
function changeExpressMoney(){
	var totalWeight = 0;
	var goods = $(".goods-box-each");
	var goodArray = [];
	for(var i = 0; i < goods.length; i++) {
		var teaBox = $(goods[i]).find(".item-box")[0];
		var teaWeight = $(teaBox).find("input:eq(0)").val();
		totalWeight += parseInt(teaWeight);
	}
	var expressType = $("#expressType").val();
	var recipientProvice = $("#recipientProvice").val();
	var expressMoney = evalExpressMoney(expressType,totalWeight,recipientProvice);
	$("#expressMoney").text(expressMoney);
	updateTotalMoney();
}

/**
 * 计算快递费. 
 * @param {Object} expressType
 * @param {Object} totalWeight
 */
function evalExpressMoney(expressType, totalWeight,recipientProvice){
	//TODO 快递费计算需要完善，可以做成配置类型
	var money= 20;
	var weight = totalWeight * 500; //转换为g
	if(expressType == 1){
		if(weight < 1000){
			money = 10;
		}
	}
	return money;
}

/**
 * 更新总金额
 */
function updateTotalMoney(){
	var money = 0;
	var goods = $(".goods-box-each");
	if(Util.isEmpty(goods)){
		return;
	}
	var goodArray = [];
	for(var i = 0; i < goods.length; i++) {
		var teaBox = $(goods[i]).find(".item-box")[0];
		var teaMoney = $(teaBox).find("input:eq(1)").val();
		var wrapBox = $(goods[i]).find(".item-box")[1];
		var wrapMoney = $(wrapBox).find("input:eq(1)").val();
		if(!Util.isEmpty(teaMoney)){
			money += parseFloat(teaMoney)
		}
		if(!Util.isEmpty(wrapMoney)){
			money+= parseFloat(wrapMoney);
		}
	}
	money += parseFloat($("#expressMoney").text());
	if(money == NaN){
		return ;
	}
	$("#totalMaoney").text(money);
}
